package gob.osinerg.emergencia.tag;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang.StringUtils;


public class ComboBoxControl  extends GenericControl{
	
	private String OPEN_TAG="<select";
	private String CLOSE_TAG="</select>";
	private List listaOpciones = new ArrayList();
	private String DEFAULT_OPTION="<option value=\"\">-- Seleccione --</option>";
	private String selectedValue;

	
	public String toString() {
		StringBuffer sb = new StringBuffer();
		sb.append(getLabel()+" ");
		sb.append(OPEN_TAG);
		addElements(sb, "name", getName());
		addElements(sb, "onchange", getOnchange());
		sb.append(">");
		sb.append(DEFAULT_OPTION);
		addOptions(sb);
		sb.append(CLOSE_TAG);
		return sb.toString();
	}
	
	public void addOptions(StringBuffer sb){
		String OPEN_OPTION="<option";
		String CLOSE_OPTION="</option>";
		try{
			for(int i=0;i<getListaOpciones().size();i++){
					Object obj = getListaOpciones().get(i);
					sb.append(OPEN_OPTION);
					addElements(sb, "value", BeanUtils.getProperty(obj, "VALUE")+"_"+BeanUtils.getProperty(obj, "DESCRIPCION")+"_"+getId());
					if (!StringUtils.isEmpty(getSelectedValue())){
						if(getSelectedValue().equals(BeanUtils.getProperty(obj, "VALUE")+"_"+BeanUtils.getProperty(obj, "DESCRIPCION")+"_"+getId())){
							addElements(sb, "selected", StringUtils.isEmpty(getSelectedValue())?null:"selected");
						}
					}
					sb.append(">");
					sb.append(BeanUtils.getProperty(obj, "DESCRIPCION"));
					sb.append(CLOSE_OPTION);
			}
		}catch (Exception e) {
			log.error(e);
		}
	}

	public List getListaOpciones() {
		return listaOpciones;
	}

	public void setListaOpciones(List listaOpciones) {
		this.listaOpciones = listaOpciones;
	}

	public String getSelectedValue() {
		return selectedValue;
	}

	public void setSelectedValue(String selectedValue) {
		this.selectedValue = selectedValue;
	}


	
	
}
